﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SHJunketingModel;

namespace SHJunketingDAL
{
    public static partial class C_UserRoleService
    {
        public static C_UserRole AddC_UserRole(C_UserRole c_UserRole)
        {
            string sql =
                "INSERT C_UserRole (UserId,RoleId)" +
                "VALUES (@UserId,@RoleId)";

            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@UserId", c_UserRole.UserId),
                    new SqlParameter("@RoleId", c_UserRole.RoleId)
				};

                int newId = DBHelper.GetScalar(sql, para);
                return GetC_UserRoleByRoleId(newId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public static void DeleteC_UserRole(C_UserRole c_UserRole)
        {
            DeleteC_UserRoleByRoleId(c_UserRole.RoleId);
        }

        /// <summary>
        /// 删除  刘涛
        /// </summary>
        /// <param name="userId"></param>
        public static void DeleteC_userRoles(string userId) 
        {
            string sql = "DELETE C_UserRole WHERE userId in (" + userId + ")";

            try
            {

                DBHelper.ExecuteCommand(sql);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public static void DeleteC_UserRoleByRoleId(int roleId)
        {
            string sql = "DELETE C_UserRole WHERE RoleId = @RoleId";

            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@RoleId", roleId)
				};

                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public static void ModifyC_UserRole(C_UserRole c_UserRole)
        {
            string sql =
                "UPDATE C_UserRole " +
                "SET " +
                    "UserId = @UserId " +
                "WHERE RoleId = @RoleId";


            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@RoleId", c_UserRole.RoleId),
					new SqlParameter("@UserId", c_UserRole.UserId)
				};

                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }

        public static IList<C_UserRole> GetAllC_UserRoles()
        {
            string sqlAll = "SELECT * FROM C_UserRole";
            return GetC_UserRolesBySql(sqlAll);
        }

        public static C_UserRole GetC_UserRoleByRoleId(int roleId)
        {
            string sql = "SELECT * FROM C_UserRole WHERE RoleId = @RoleId";

            try
            {
                SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@RoleId", roleId));
                if (reader.Read())
                {
                    C_UserRole c_UserRole = new C_UserRole();

                    c_UserRole.RoleId = (int)reader["RoleId"];
                    c_UserRole.UserId = (int)reader["UserId"];

                    reader.Close();

                    return c_UserRole;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }




        private static IList<C_UserRole> GetC_UserRolesBySql(string safeSql)
        {
            List<C_UserRole> list = new List<C_UserRole>();

            try
            {
                DataTable table = DBHelper.GetDataSet(safeSql);

                foreach (DataRow row in table.Rows)
                {
                    C_UserRole c_UserRole = new C_UserRole();

                    c_UserRole.RoleId = (int)row["RoleId"];
                    c_UserRole.UserId = (int)row["UserId"];

                    list.Add(c_UserRole);
                }

                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }

        private static IList<C_UserRole> GetC_UserRolesBySql(string sql, params SqlParameter[] values)
        {
            List<C_UserRole> list = new List<C_UserRole>();

            try
            {
                DataTable table = DBHelper.GetDataSet(sql, values);

                foreach (DataRow row in table.Rows)
                {
                    C_UserRole c_UserRole = new C_UserRole();

                    c_UserRole.RoleId = (int)row["RoleId"];
                    c_UserRole.UserId = (int)row["UserId"];

                    list.Add(c_UserRole);
                }

                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }


        /// <summary>
        /// 查找用户权限。刘涛
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public static IList<C_UserRole> GetUsrePermissions(int userId)
        {
            string sql = "select * from c_userrole where userId = " + userId;
            return GetC_UserRolesBySql(sql);
        }


        /// <summary>
        /// 查找用户。刘涛
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public static IList<C_UserRole> GetUsreId(int roleId)
        {
            string sql = "select * from c_userrole where RoleId = " + roleId;
            return GetC_UserRolesBySql(sql);
        }
    }
}
